package com.relytosoft.mizar.media.camera.services.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.relytosoft.dubhe.common.core.base.page.PageQuery;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.dubhe.common.core.utils.BeanCopyUtils;
import com.relytosoft.dubhe.common.core.utils.CommonUtils;
import com.relytosoft.dubhe.common.log.annotation.OpLog;
import com.relytosoft.dubhe.common.log.enums.OpTypeEnum;
import com.relytosoft.mizar.media.camera.api.entity.CameraChannel;
import com.relytosoft.mizar.media.camera.api.pojo.dto.CameraChannelDto;
import com.relytosoft.mizar.media.camera.api.pojo.vo.CameraChannelVo;
import com.relytosoft.mizar.media.camera.services.service.CameraChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author lj
 */
@Api(tags = {"通道"})
@RequestMapping("/camera/channel")
@RestController
public class CameraChannelController {

    @Resource
    private CameraChannelService cameraChannelService;


    @SaCheckPermission("camera:channel:sync")
    @OpLog(msg = "通道同步", opType = OpTypeEnum.INSERT)
    @ApiOperation("同步")
    @GetMapping("sync/{type}")
    public ResponseResult<Void> sync(@PathVariable Integer type) {
        cameraChannelService.channelPage(type);
        return ResponseResult.ok("数据已开始同步，稍后刷新查看");
    }

    @SaCheckPermission("camera:channel:list")
    @OpLog(msg = "通道列表", opType = OpTypeEnum.SELECT)
    @ApiOperation("查询")
    @GetMapping("/pagelist")
    public ResponseResult<Map<String, Object>> selectChannelPageList(PageQuery pageQuery, CameraChannelDto cameraChannelDto) {
        IPage<CameraChannelVo> page = cameraChannelService.selectChannelPageList(pageQuery, cameraChannelDto);
        return ResponseResult.ok(CommonUtils.getPageData(page));
    }

    @SaCheckPermission("camera:channel:update")
    @OpLog(msg = "通道修改", opType = OpTypeEnum.UPDATE)
    @ApiOperation("修改")
    @PutMapping
    public ResponseResult<Void> update(@RequestBody CameraChannelDto cameraChannelDto) {
        CameraChannel cameraChannel = BeanCopyUtils.copy(cameraChannelDto, CameraChannel.class);
        boolean b = cameraChannelService.updateCameraChannel(cameraChannel);
        return b ? ResponseResult.ok("修改成功") : ResponseResult.fail("修改失败");
    }

    @SaCheckPermission("camera:channel:id")
    @OpLog(msg = "通道详情", opType = OpTypeEnum.SELECT)
    @ApiOperation("详情")
    @GetMapping("{id}")
    public ResponseResult<CameraChannelVo> byId(@PathVariable Integer id) {
        return ResponseResult.ok(cameraChannelService.byId(id));
    }

    @SaCheckPermission("camera:channel:template")
    @OpLog(msg = "码流模板", opType = OpTypeEnum.OTHER)
    @ApiOperation("获取模板")
    @GetMapping("/template")
    public ResponseResult<String> getRtspTemplate(Integer cameraId, String template) {
        return ResponseResult.ok(cameraChannelService.getRtspTemplate(cameraId, template), "成功");
    }
}
